package org.jeecg.modules.scm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.flow.api.FlowBaseCloudApi;
import org.jeecg.modules.flow.query.TaskQuery;
import org.jeecg.modules.flow.vo.TaskVo;
import org.jeecg.modules.scm.dto.EditReceivablesDto;
import org.jeecg.modules.scm.dto.RelevancyInvQuery;
import org.jeecg.modules.scm.entity.ScmFinaContract;
import org.jeecg.modules.scm.entity.ScmReceivables;
import org.jeecg.modules.scm.entity.ScmReceivablesInvoice;
import org.jeecg.modules.scm.mapper.ScmCloudChainMapper;
import org.jeecg.modules.scm.mapper.ScmReceivablesMapper;
import org.jeecg.modules.scm.service.IScmReceivablesInvoiceService;
import org.jeecg.modules.scm.service.IScmReceivablesService;
import org.jeecg.modules.scm.vo.InvoiceRelevancyVo;
import org.jeecg.modules.scm.vo.ReceivablesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 应收账款控制器
 * @Author: zzq
 * @Date: 2021-10-25
 * @Version: V1.0
 */
@Api(tags = "应收账款管理")
@RestController
@RequestMapping("/scm/receivables")
@Slf4j
public class ScmReceivablesController extends JeecgController<ScmReceivables, IScmReceivablesService> {
    @Autowired
    private IScmReceivablesService scmReceivablesService;

    @Autowired
    private IScmReceivablesInvoiceService scmReceivablesInvoiceService;

    @Autowired
    private ScmReceivablesMapper scmReceivablesMapper;

    @Resource
    private FlowBaseCloudApi flowBaseCloudApi;

    @Autowired
    private ScmCloudChainMapper scmCloudChainMapper;

    /**
     * 分页列表查询
     *
     * @param scmReceivables
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "scm_receivables-分页列表查询")
    @ApiOperation(value = "分页列表查询", notes = "scm_receivables-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ReceivablesVo>> queryPageList(ScmReceivables scmReceivables,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<ScmReceivables> queryWrapper = QueryGenerator.initQueryWrapper(scmReceivables, req.getParameterMap());
        Page<ReceivablesVo> page = new Page<>(pageNo, pageSize);
        IPage<ReceivablesVo> pageList = scmReceivablesMapper.queryPage(queryWrapper, page);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "scm_receivables-添加")
    @ApiOperation(value = "添加", notes = "scm_receivables-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody EditReceivablesDto dto) {
        Integer id = scmReceivablesService.saveByDto(dto);
        return Result.OK("添加成功！", id);
    }

    /**
     * 编辑
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "scm_receivables-编辑")
    @ApiOperation(value = "编辑", notes = "scm_receivables-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody EditReceivablesDto dto) {
        boolean isOk = scmReceivablesService.updateByDto(dto);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "scm_receivables-通过id删除")
    @ApiOperation(value = "通过id删除", notes = "scm_receivables-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        scmReceivablesService.removeById(id);
        return Result.OK("删除成功!");
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "scm_receivables-通过id查询")
    @ApiOperation(value = "通过id查询", notes = "scm_receivables-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        ScmReceivables scmReceivables = scmReceivablesService.getById(id);
        if (scmReceivables == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(scmReceivables);
    }

    /**
     * 上传合同
     *
     * @param id
     * @return
     */
    @AutoLog(value = "scm_receivables-上传贸易合同")
    @ApiOperation(value = "贸易合同上传", notes = "scm_receivables-贸易合同上传")
    @PostMapping(value = "/uploadContract")
    public Result<?> uploadContract(@RequestParam MultipartFile file,
                                    @RequestParam Integer id,
                                    @RequestParam String no) {
        String path = scmReceivablesService.uploadContract(file, no, id);
        return Result.OK(path);
    }

    /**
     * 删除合同
     *
     * @param id
     * @return
     */
    @AutoLog(value = "scm_receivables-删除贸易合同")
    @ApiOperation(value = "贸易合同上传", notes = "删除贸易合同")
    @PostMapping(value = "/deleteContract")
    public Result<Boolean> deleteContract(@RequestParam Integer id) {
        Boolean deleted = scmReceivablesService.deleteContract(id);
        if (!deleted) {
            return Result.error("删除失败", false);
        }
        return Result.OK("删除成功", true);
    }

    /**
     * 上传其他材料
     *
     * @param id
     * @return
     */
    @AutoLog(value = "scm_receivables-其他支持材料上传")
    @ApiOperation(value = "其他支持材料上传", notes = "scm_receivables-其他支持材料上传")
    @PostMapping(value = "/uploadAccessory")
    public Result<String[]> uploadAccessory(@RequestParam MultipartFile file,
                                            @RequestParam Integer id) {
        String path = scmReceivablesService.uploadAccessory(file, id);
        return Result.OK(path.split(","));
    }

    @AutoLog(value = "scm_receivables-关联发票")
    @PostMapping(value = "/relevancyInvoice/")
    @ApiOperation(value = "应收账款关联发票")
    public Result<InvoiceRelevancyVo> relevancyInvoice(@RequestBody @Validated RelevancyInvQuery query) {
        return scmReceivablesService.relevancyInvoice(Integer.valueOf(query.getId()), query.getInvoiceNo());
    }

    @AutoLog(value = "scm_receivables-解除发票关联")
    @PostMapping(value = "/disassociation")
    @ApiOperation(value = "应收账款解除关联发票")
    public Result<?> disassociation(@RequestParam Integer id,
                                    @RequestBody List<String> invoiceNos) {
        scmReceivablesService.disassociation(id, invoiceNos);
        return Result.OK("解除成功");
    }


    @AutoLog(value = "scm_receivables-查询应收账款关联的发票列表")
    @PostMapping(value = "/queryInvoiceById")
    @ApiOperation(value = "查询应收账款关联的发票列表")
    public Result<List<ScmReceivablesInvoice>> queryInvoiceById(@RequestParam Integer id) {
        List<ScmReceivablesInvoice> list = scmReceivablesInvoiceService.getByReceivableId(id);
        return Result.OK(list);
    }

    @ApiOperation(value = "申请生成云链单", notes = "scm_receivables-申请生成云链单")
    @PostMapping(value = "/applyCloudChain")
    public Result<String> applyCloudChain(@RequestParam Integer id) {
        String instanceId = scmReceivablesService.applyCloudChain(id);
        return Result.OK("已启动申请流程", instanceId);
    }


    @ApiOperation(value = "查询当前用户应收账款申请任务")
    @GetMapping(value = "/findReceivablesTaskList")
    public Result<Page<TaskVo>> findTaskList(TaskQuery query,
                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return flowBaseCloudApi.findTasks(query, pageNo, pageSize);
    }

    @ApiOperation(value = "根据应收账款ID查询融资合同信息")
    @GetMapping(value = "/queryFinalContractByReceivablesId")
    public Result<ScmFinaContract> queryFinalContractByReceivablesId(Integer receivableId) {
        ScmFinaContract scmFinaContract = scmCloudChainMapper.queryFinalContractByReceivablesId(receivableId);
        if (scmFinaContract == null) {
            return Result.error("无数据", null);
        }
        return Result.OK(scmFinaContract);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param scmReceivables
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ScmReceivables scmReceivables) {
        return super.exportXls(request, scmReceivables, ScmReceivables.class, "scm_receivables");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ScmReceivables.class);
    }

    @ApiOperation("超时测试")
    @GetMapping("/timeOutTest")
    public Result<?> timeOut(Long time) {
        final Result<?> result = flowBaseCloudApi.timeOut(time);
        log.info(result.toString());
        return result;
    }

}
